package MyLinkList;

public class LinkList {

    static class Node {
        public int num;
        public Node next;

        public Node(int num) {
            this.num = num;
        }
    }

    public Node head;

    public void createList(){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(4);
        Node node4 = new Node(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }

    public void display(){
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.num + " ");
            cur = cur.next;
        }
    }

    public void addFirst(int num){
        Node node = new Node(num);
        node.next = head;
        head = node;
    }

    public void addLast(int num) {
        Node node = new Node(num);
        if (head == null) {
            head = node;
        } else {
            Node cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    public void addIndex(int pos , int num) {
        Node node = new Node(num);
        Node cur = head;
        int count = 1;
        if(pos == 0) {
            addFirst(num);
            return;
        } else if (pos == 1) {
            node.next = cur.next;
            cur.next = node;
            return;
        }
        while (count != pos-1) {
            cur = cur.next;
            count++;
        }
        node.next = cur.next;
        cur.next = node;
    }

    private Node findIndexSubOne(int index) {
        int count = 0;
        Node cur = head;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public boolean contains(int val) {
        Node cur = head;
        while (cur.next != null) {
            if(cur.num == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void removeFirst(int val) {
        if (head == null) {
            System.out.println("没有可以删除的结点！");
            return;
        }
        Node cur = head;
        if (cur.num == val) {
            head = cur.next;
            return;
        }
        while (cur.next.num != val && cur.next.next != null) {
            cur = cur.next;
        }
        if(cur.next.next != null){
            cur.next = cur.next.next;
        } else {
            System.out.println("找不到！ ");
        }
    }

    public void removeAllKey(int val) {
        if (head == null) {
            return;
        }
        Node prev = head;
        Node cur = head.next;
        while (cur != null) {
            if (cur.num == val) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.num == val) {
            head = head.next;
        }
    }
}
